package com.ruoyi.web.controller.net;

import com.ruoyi.net.domain.ElementPortEth;
import com.ruoyi.net.domain.ElementPortEthSub;
import com.ruoyi.net.domain.NetElement;
import com.ruoyi.net.netconf.util.DeviceManager;
import com.ruoyi.net.service.IElementPortEthService;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.net.domain.ElementPortEthL3;
import com.ruoyi.net.service.IElementPortEthL3Service;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 网元-接口-以太网L3Controller
 * 
 * @author net
 * @date 2024-11-17
 */
@Api(tags = "网元-接口-以太网L3")
@RestController
@RequestMapping("/element/port/eth/l3")
public class ElementPortEthL3Controller extends BaseController {
    @Autowired
    private IElementPortEthL3Service elementPortEthL3Service;

    @Autowired
    private IElementPortEthService elementPortEthService;

    @Autowired
    private INetElementService netElementService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private DeviceManager deviceManager;

    /**
     * 查询网元-接口-以太网L3列表
     */
    @ApiOperation(value = "查询网元-接口-以太网L3列表", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('system:l3:list')")
    @GetMapping("/list")
    public TableDataInfo list(ElementPortEthL3 elementPortEthL3) {
        startPage();
        List<ElementPortEthL3> list = elementPortEthL3Service.selectElementPortEthL3List(elementPortEthL3);
        return getDataTable(list);
    }

    /**
     * 导出网元-接口-以太网L3列表
     */
    @ApiOperation(value = "导出网元-接口-以太网L3列表", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('system:l3:export')")
    @Log(title = "网元-接口-以太网L3", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElementPortEthL3 elementPortEthL3) {
        List<ElementPortEthL3> list = elementPortEthL3Service.selectElementPortEthL3List(elementPortEthL3);
        ExcelUtil<ElementPortEthL3> util = new ExcelUtil<ElementPortEthL3>(ElementPortEthL3.class);
        util.exportExcel(response, list, "网元-接口-以太网L3数据");
    }

    /**
     * 获取网元-接口-以太网L3详细信息
     */
    @ApiOperation(value = "获取网元-接口-以太网L3详细信息", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('system:l3:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(elementPortEthL3Service.selectElementPortEthL3ById(id));
    }

    /**
     * 新增网元-接口-以太网L3
     */
    @ApiOperation(value = "新增网元-接口-以太网L3", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('system:l3:add')")
    @Log(title = "网元-接口-以太网L3", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElementPortEthL3 elementPortEthL3) {
        sendXml(elementPortEthL3);
        return toAjax(elementPortEthL3Service.insertElementPortEthL3(elementPortEthL3));
    }

    /**
     * 修改网元-接口-以太网L3
     */
    @ApiOperation(value = "修改网元-接口-以太网L3", httpMethod = "PUT")
    //    @PreAuthorize("@ss.hasPermi('system:l3:edit')")
    @Log(title = "网元-接口-以太网L3", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElementPortEthL3 elementPortEthL3) {
        sendXml(elementPortEthL3);
        return toAjax(elementPortEthL3Service.updateElementPortEthL3(elementPortEthL3));
    }

    /**
     * 删除网元-接口-以太网L3
     */
    @ApiOperation(value = "删除网元-接口-以太网L3", httpMethod = "DELETE")
    //    @PreAuthorize("@ss.hasPermi('system:l3:remove')")
    @Log(title = "网元-接口-以太网L3", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(elementPortEthL3Service.deleteElementPortEthL3ByIds(ids));
    }

    /// / TODO: 2024/11/30

    private void sendXml(ElementPortEthL3 entity) {
        // TODO: 2024/11/30 设备联调
        if (sysConfigService.selectDebugEnabled()) {
            NetElement netElement = netElementService.getCacheObject(entity.getElementId());
            deviceManager.getDevice(netElement.getIp());

            ElementPortEth elementPortEth = elementPortEthService.selectElementPortEthById(entity.getPortEthId());
            String xml = "<if xmlns=\"http://fiberhome.com/fhnw/yang/interface/fos-interface-common\">" +
                    "    <interfaces>" +
                    "      <interface xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" nc:operation=\"" + ("1".equalsIgnoreCase(entity.getStatus()) ? "create" : "remove") + "\">" +
                    "        <name>" + elementPortEth.getPortName() + "</name>" +
                    "        <type>ETH-PHY</type>" +
                    "        <enabled>enable</enabled>" +
                    "        <ip xmlns=\"http://fiberhome.com/fhnw/yang/interface/fos-interface-l2l3common\">" +
                    "          <ipv4-mtu>1500</ipv4-mtu>" +
                    "           <ipv4-address-list>" +
                    "               <ipv4-address>" + entity.getIp() + "/" + entity.getMask() + "</ipv4-address>" +
                    "               <secondry-use>mast</secondry-use>" +
                    "               </ipv4-address-list>" +
                    "        </ip>" +
                    "      </interface>" +
                    "    </interfaces>" +
                    "  </if>";
            System.out.println(xml);
            deviceManager.editConfig(xml, "merge");
        }
    }
}